/**
 * Created by asus on 2018/9/18.
 */
define(['jquery', 'cookie'], function($) {
    var timer;
    var counter = 60;
    var isValidate = true
    var inputItems = $('.input-item input')
    var phone = $("[name='phone']"),
        password = $("[name='password']")
    username = $("[name='username']")
    code = $("[name='code']")
    var way = 'byCode'
    var identiy = "regularUser"
    // staffUser 恒瑞员工
    // regularUser 普通用户
    // tab
    $(".tab-item").click(function() {
        identiy = $(this).data("identiy")
        $(this).addClass("active").siblings().removeClass("active")
    })


    // 密码正则
    var regPswd = /(?!^[0-9]+$)(?!^[A-z]+$)(?!^[^A-z0-9]+$)^.{6,20}$/
    // 手机号码正则
    var regPhone = /^[1][3,4,5,7,8][0-9]{9}$/
    // 必填项校验
    inputItems.blur(function() {
        if (!$(this).val()) {
            isValidate = false
            $(this).siblings('.errTip.required').show()
        }
    })
    inputItems.focus(function() {
        $(this).siblings('.errTip').hide()
    })
    $('.input-item .errTip').click(function() {
        $(this).siblings('input').trigger('focus')
    })
    // 选择登录方式
    $('.radio-group .radio').click(function() {
        $('.byWay').hide()
        way = $(this).find('.radio__original').val()
        $('.' + way).show()
        $('.radio__input').removeClass('is-checked')
        $(this).children('.radio__input').addClass('is-checked')
        return false
    })

    // 点击发送验证码
    $('.messgae-btn').click(function() {
        if ($(this).attr("disabled")) {
            return
        }
        var mobilePhone = phone.val();
        if (!mobilePhone) {
            $('.phone').siblings('.errTip.required').show()
            return
        } else if (!(regPhone.test(mobilePhone))) {
            $('.phone').siblings('.errTip.formatter').show()
            return
        }
        setTimer($(this))
    })


    // 提交表单
    $(".login-btn").click(function() {
        var mockData = {
            id: '192984358',
            role: 1 // 1 为医生 2 为药代
        }
        var expiresDate = new Date();
        expiresDate.setTime(expiresDate.getTime() + (60 * 60 * 1000));
        $.cookie('userToken', JSON.stringify(mockData), {
            path: '/',
            expires: expiresDate
        })
        var data = {}
        if (!validateForm()) {
            return
        }
        if (way === 'byCode') {
            data = {
                phone: phone.val(),
                code: code.val()
            }
        } else {
            data = {
                username: username.val(),
                password: password.val(),
            }
        }

        $.ajax({
            url: '/login',
            type: 'POST',
            data: data,
            success: function(res) {
                console.log(res)
                window.location.href = "dashboard.html"
            }
        })

    })



    // 表单校验
    function validateForm() {
        isValidate = true
        // 校验是否为空
        if (way === 'byCode') {
            phone.trigger('blur')
            code.trigger('blur')
        } else {
            username.trigger('blur')
            password.trigger('blur')
        }
        if (!isValidate) {
            return
        }
        if (phone.val() && !(/^1[0123456789]\d{9}$/.test(phone.val()))) {
            phone.siblings('.errTip.formatter').show()
            return
        }
        // if (way === 'byPwsd' && !regPswd.test(password.val())) {
        //   $('.toast').css('display', 'block').html('密码由6位以上字母、数字和符号两种及以上的组合').fadeIn().fadeOut(2000);
        //   return
        // }
        // 校验短信验证码
        //if (way === 'byCode') {
        //  $('.toast').css('display', 'block').html('密码由6位以上字母、数字和符号两种及以上的组合').fadeIn().fadeOut(2000);
        //  return
        //}

        return true

    }

    function setTimer($el) {
        if (timer) {
            clearTimeout(timer);
        }
        if (counter === 0) {
            $el.attr("disabled", false);
            $el.html("重获验证码");
            $el.css({
                color: "#666",
                "border-color": "#e7e7e7"
            });
            counter = 60
        } else {
            $el.attr("disabled", true);
            $el.css({
                color: "#ccc",
                "border-color": "#e7e7e"
            });
            $el.html("重获验证码(" + counter + "s)");
            counter--;
        }
        timer = setTimeout(function() {
            setTimer($el);
        }, 1000);
    }

})